package com.sscl.baselibrary.exts

import android.app.Dialog
import android.content.Context
import android.os.Build
import android.view.View
import android.view.WindowInsets
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager


/**
 * 隐藏导航栏,并让对话框全屏
 */
fun Dialog.hideNavigationBar() {
    //隐藏导航栏
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        this.window?.decorView?.windowInsetsController?.hide(WindowInsets.Type.navigationBars())
        this.window?.decorView?.windowInsetsController?.hide(WindowInsets.Type.statusBars())
        this.window?.decorView?.windowInsetsController?.hide(WindowInsets.Type.captionBar())
        this.window?.decorView?.windowInsetsController?.hide(WindowInsets.Type.systemBars())
        this.window?.decorView?.windowInsetsController?.hide(WindowInsets.Type.displayCutout())
        this.window?.decorView?.windowInsetsController?.hide(WindowInsets.Type.mandatorySystemGestures())
        this.window?.decorView?.windowInsetsController?.hide(WindowInsets.Type.systemGestures())
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            this.window?.decorView?.windowInsetsController?.hide(WindowInsets.Type.systemOverlays())
        }
        this.window?.decorView?.windowInsetsController?.hide(WindowInsets.Type.tappableElement())
    } else {
        @Suppress("DEPRECATION")
        window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        @Suppress("DEPRECATION")
        window?.decorView?.setOnSystemUiVisibilityChangeListener {
            var uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    //布局位于状态栏下方
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    //全屏
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    //隐藏导航栏
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            uiOptions = uiOptions or 0x00001000
            window?.decorView?.systemUiVisibility = uiOptions
        }
        // 设置全屏的参数(只有低安卓版本才需要额外设置这一步)
        val attributes = window?.attributes
        @Suppress("DEPRECATION")
        attributes?.flags = attributes?.flags?.or(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window?.attributes = attributes
    }
}

/**
 * 隐藏输入法
 */
fun Dialog.hideSoftInputMethod() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    // 隐藏输入法
    imm.hideSoftInputFromWindow(window?.decorView?.windowToken, 0)
    // 禁用输入法
    window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
}